#include <sys_config.h>

#ifdef DVR_PVR_SUPPORT

#include <types.h>
#include <osal/osal.h>
#include <basic_types.h>
#include <mediatypes.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <osal/osal.h>
#include <hld/hld_dev.h>

#include <api/libosd/osd_lib.h>

#include "osdobjs_def.h"

#include "string.id"
#include "images.id"
#include "osd_config.h"
#include "osd_rsc.h"

#include "copper_common/system_data.h"
#include "copper_common/menu_api.h"
#include "win_com_menu_define.h"
#include "win_com.h"

#include <hld/dmx/dmx_dev.h>
#include <hld/dmx/dmx.h>
#include <hld/decv/vdec_driver.h>
#ifndef NEW_DEMO_FRAME
#include <api/libpub29/lib_hde.h>
#include <api/libpub29/lib_pub27.h>
#else
#include <api/libpub/lib_hde.h>
#endif

#include "menus_root.h"

/*******************************************************************************
*	Objects definition
*******************************************************************************/
extern CONTAINER g_win_jumpstep;

extern CONTAINER jumpstep_item_con1;
extern TEXT_FIELD jumpstep_item_txtname1;
extern TEXT_FIELD jumpstep_item_txtset1;
extern TEXT_FIELD jumpstep_item_line1;


static VACTION jumpstep_item_con_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT jumpstep_item_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static VACTION jumpstep_con_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT jumpstep_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);


#define LDEF_CON(root, varCon,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,conobj,focusID)		\
    DEF_CONTAINER(varCon,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, CON_SH_IDX,CON_HL_IDX,CON_SL_IDX,CON_GRY_IDX,   \
    jumpstep_item_con_keymap,jumpstep_item_con_callback,  \
    conobj, focusID,1)

#define LDEF_TXTNAME(root,varTxt,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,resID,str)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, TXTN_SH_IDX,TXTN_HL_IDX,TXTN_SL_IDX,TXTN_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,resID,str)

#define LDEF_TXTSET(root,varTxt,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,resID,str)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, TXTS_SH_IDX,TXTS_HL_IDX,TXTS_SL_IDX,TXTS_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_CENTER| C_ALIGN_VCENTER, 20,0,resID,str)


#define LDEF_MM_ITEM(root,varCon,nxtObj,varTxtName,varTxtset,varLine,ID,IDu,IDd,l,t,w,h,nameID,setstr)	\
	LDEF_CON(&root,varCon,nxtObj,ID,ID,ID,IDu,IDd,l,t,w,h,&varTxtName,1)	\
	LDEF_TXTNAME(&varCon,varTxtName,&varTxtset,0,0,0,0,0,l + TXTN_L_OF, t + TXTN_T_OF,TXTN_W,TXTN_H,nameID,NULL)	\
	LDEF_TXTSET(&varCon,varTxtset,NULL/*&varLine*/        ,1,1,1,1,1,l + TXTS_L_OF, t + TXTS_T_OF,TXTS_W,TXTS_H,0,setstr) \
	LDEF_LINE(&varCon, varLine, NULL, 1, 1, 1, 1, 1, l + LINE_L_OF, t + TXTS_H, LINE_W, LINE_H, 0, 0)

LDEF_MM_ITEM(g_win_jumpstep,jumpstep_item_con1, NULL,jumpstep_item_txtname1,jumpstep_item_txtset1,jumpstep_item_line1,1,1,1,	\
				CON_L, CON_T + (CON_H + CON_GAP)*0,CON_W,CON_H,	RS_DVR_JUMP,display_strs[0])

DEF_CONTAINER(g_win_jumpstep,NULL,NULL,C_ATTR_ACTIVE,0, \
    1,0,0,0,0, W_L,W_T,W_W,W_H, WIN_SH_IDX,WIN_HL_IDX,WIN_SL_IDX,WIN_GRY_IDX,   \
    jumpstep_con_keymap,jumpstep_con_callback,  \
    (POBJECT_HEAD)&jumpstep_item_con1, 1,0)


/*******************************************************************************
*	Local functions & variables define
*******************************************************************************/
CONTAINER* jumpstep_items[] =
{
	&jumpstep_item_con1,
};

POBJECT_HEAD jumpstep_item_name[] =
{
	(POBJECT_HEAD)&jumpstep_item_txtname1,
};

POBJECT_HEAD jumpstep_item_set[] =
{
	(POBJECT_HEAD)&jumpstep_item_txtset1,
};


void win_jumpstep_set_string(UINT8 input)
{
	UINT8 temp[10];
	switch(input)
	{
		case 0:
			sprintf(temp, "30 sec", input);
			break;
		case 1:
			sprintf(temp, "1 min", input);
			break;
		case 2:
			sprintf(temp, "5 min", input);
			break;
		case 3:
			sprintf(temp, "10 min", input);
			break;
		case 4:
			sprintf(temp, "30 min", input);
			break;
		default:
			break;			
	}
	ComAscStr2Uni(temp, display_strs[0]);
}

/*******************************************************************************
*	key mapping and event callback definition
*******************************************************************************/

static VACTION jumpstep_item_con_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	
	switch(key)
	{
	case V_KEY_RIGHT:
		act = VACT_INCREASE;
		break;		
	case V_KEY_LEFT:		
		act = VACT_DECREASE;
		break;		
	case V_KEY_ENTER:
		act = VACT_ENTER;
		break;		
	default:	
		act = VACT_PASS;
		break;
	}

	return act;

}


PRESULT comlist_menu_jumpstep_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	UINT16 i,wTop;
	UINT8 id;
	char str_len;
    	OBJLIST* ol;
	UINT8 temp[4];
	
	PRESULT cbRet = PROC_PASS;

    	ol = (OBJLIST*)pObj;
	
	if(event==EVN_PRE_DRAW)
	{
	}
	else if(event == EVN_POST_CHANGE)
		cbRet = PROC_LEAVE;

	return cbRet;
}

static PRESULT jumpstep_item_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	UINT8	unact, input;
	UINT8	id = OSD_GetObjID(pObj);

	switch(event)
	{		
	case EVN_FOCUS_PRE_GET:
		break;
		
	case EVN_FOCUS_PRE_LOSE:
		break;

	case EVN_UNKNOWN_ACTION:
		unact = (VACTION)(param1>>16) ;
		input = sys_data_get_jumpstep();	
		if(unact == VACT_INCREASE)
		{
			input = (input+1)%5;
		}
		else if(unact == VACT_DECREASE)
		{
			if(!input)
				input = 5;
			input = (input-1)%5;
		}
		sys_data_set_jumpstep(input);
		win_jumpstep_set_string(input);
		OSD_TrackObject(pObj,C_UPDATE_ALL);
		ret = PROC_LOOP;
		break;
	}
	return ret;

}

static VACTION jumpstep_con_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	
	switch(key)
	{
	case V_KEY_EXIT:
	case V_KEY_MENU:
		act = VACT_CLOSE;
		break;
	default:	
		act = VACT_PASS;
		break;
	}

	return act;
}

static PRESULT jumpstep_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	UINT8	unact;
	UINT32 i;
	BOOL subt_flag;
	CONTAINER* cont = (CONTAINER*)pObj;
	UINT8	id = OSD_GetObjID(pObj);
	static UINT8 preval;
	
	switch(event)
	{
	case EVN_PRE_OPEN:
		preval = sys_data_get_jumpstep();
		wincom_open_title((POBJECT_HEAD)&g_win_jumpstep,RS_DVR_JUMP, 0);
		win_jumpstep_set_string(preval);
		break;
	
	case EVN_POST_OPEN:
		break;
		
	case EVN_PRE_CLOSE:
        *((UINT32 *)param2) &= ~C_CLOSE_CLRBACK_FLG;
		if(preval != sys_data_get_jumpstep())
		{
			sys_data_save(1);
		}
		break;

	case EVN_POST_CLOSE:
		break;
	case EVN_MSG_GOT:
#ifdef DISK_MANAGER_SUPPORT
		if (param1 == CTRL_MSG_SUBTYPE_CMD_STO && param2 == USB_STATUS_OVER)
		{
			// if disk used by PVR detached, exit this menu
			extern pvr_play_rec_t pvr_info;
			if (!pvr_info.hdd_valid)
			{
				ret = PROC_LEAVE;
			}
		}
#endif
		break;
	default:
		break;
	}
	return ret;
}


////////////////////////////////////////////////
#endif//#ifdef DVR_PVR_SUPPORT
